#pragma once
#include <list>
#include <mutex>
#include <condition_variable>
template<class T>
class match_queue
{
private:
    //用链表而不用queue是因为有中间删除数据的存在
    std::list<T> _list;
    std::mutex _mutex;
    //条件变量防止阻塞消费者,后面使用时,队列中元素小于2则阻塞
    std::condition_variable _cond;
public:
    int size()
    {
        std::unique_lock<std::mutex> lock(_mutex);
        return _list.size();
    }
    bool empty()
    {
        std::unique_lock<std::mutex> lock(_mutex);
        return _list.empty();
    }
    //阻塞线程
    void wait()
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _cond.wait(lock);
    }
    //入队数据,并唤醒线程
    void push(const T& data)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _list.push_back(data);
        _cond.notify_all();
    }
    //出队数据
    bool pop(T& data)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        if(_list.empty() == true)
        {
            return false;
        }
        data = _list.front();
        _list.pop_front();
        return true;
    }
    //删除指定数据
    void remove(T& data)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _list.remove(data);
    }
};